package cn.mumzone.basic.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

import cn.mumzone.basic.R;


/**
 * Created by JoeLjt on 2019/7/4.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public enum PosToast {

    INSTANCE;

    private Toast mToast;

    public class ToastType {
        public static final int TOAST_TYPE_LOADING = -1;
        public static final int TOAST_TYPE_SQUARE = 0;
        public static final int TOAST_TYPE_RECTANGLE = 1;
    }

    public class ToastSquareLevel {
        public static final int ALERT_SUCCESS_SQUARE = 1000;
        public static final int ALERT_FAIL_SQUARE = 2000;
        public static final int ALERT_CHECK_MARK_SQUARE = 3000;
        public static final int ALERT_LOADING = 4000;
    }

    public class ToastRectangleLevel {
        public static final int ALERT_GUIDE_LEVEL_ONE = 1001;
        public static final int ALERT_GUIDE_LEVEL_TWO = 2001;
        public static final int ALERT_WARNING_LEVEL_ONE = 3001;
        public static final int ALERT_WARNING_LEVEL_TWO = 4001;
        public static final int ALERT_SUCCESS = 5001;
        public static final int ALERT_FAIL = 6001;
    }

    public View setToastType(LayoutInflater inflater, int type) {
        View toastView = null;
        switch (type) {
            case ToastType.TOAST_TYPE_RECTANGLE:
                toastView = inflater.inflate(R.layout.toast_for_pos_rectangle, null);
                break;
            case ToastType.TOAST_TYPE_SQUARE:
                toastView = inflater.inflate(R.layout.toast_for_pos_square, null);
                break;
        }
        return toastView;
    }

    public void setAlertLevel(Context context, View toastView, int level) {
        switch (level) {

            case ToastRectangleLevel.ALERT_GUIDE_LEVEL_ONE:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_guide_level_one,
                        R.drawable.svg_ic_toast_guide_level_one, R.color.content_text_color);
                break;
            case ToastRectangleLevel.ALERT_GUIDE_LEVEL_TWO:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_guide_level_two,
                        R.drawable.svg_ic_toast_guide_level_two, R.color.content_text_color);
                break;
            case ToastRectangleLevel.ALERT_WARNING_LEVEL_ONE:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_warning_level_one,
                        R.drawable.svg_ic_toast_alert_level_one, R.color.toast_alert_level_one_text);
                break;
            case ToastRectangleLevel.ALERT_WARNING_LEVEL_TWO:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_warning_level_two,
                        R.drawable.svg_ic_toast_alert_level_two, R.color.toast_alert_level_two_text);
                break;
            case ToastRectangleLevel.ALERT_SUCCESS:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_success_rectangle,
                        R.drawable.svg_ic_toast_success_rectangle, R.color.toast_success_text);
                break;
            case ToastRectangleLevel.ALERT_FAIL:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_fail_rectangle,
                        R.drawable.svg_ic_toast_fail_rectangle, R.color.toast_fail_text);
                break;

            case ToastSquareLevel.ALERT_SUCCESS_SQUARE:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_success_square,
                        R.drawable.svg_ic_toast_success_square, R.color.toast_success_text);
                break;
            case ToastSquareLevel.ALERT_FAIL_SQUARE:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_fail_square,
                        R.drawable.svg_ic_toast_fail_square, R.color.toast_fail_text);
                break;
            case ToastSquareLevel.ALERT_CHECK_MARK_SQUARE:
                setToastStyleByLevel(context, toastView, R.drawable.shape_bg_toast_loading,
                        R.drawable.svg_ic_toast_check_mark, R.color.white_color);
                break;
            case ToastSquareLevel.ALERT_LOADING:

                break;

        }
    }

    private void setToastStyleByLevel(Context context, View toastView, int toastBgResId, int ivDrawableResId, int color) {
        if (toastView != null) {
            toastView.setBackgroundResource(toastBgResId);
            AppCompatImageView ivToast = toastView.findViewById(R.id.iv_toast);
            ivToast.setImageDrawable(context.getResources().getDrawable(ivDrawableResId));
            TextView tvToast = toastView.findViewById(R.id.tv_toast);
            tvToast.setTextColor(context.getResources().getColor(color));
        }
    }

    @SuppressLint("ShowToast")
    public void makeToast(Context context, int toastType, int toastLevel, String text, boolean isLong) {

        if (toastType != ToastType.TOAST_TYPE_LOADING && (toastLevel - toastType) % 10 != 0) {
            throw new RuntimeException("ToastType can not match with ToastLevel!");
        }

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View toastView = setToastType(inflater, toastType);

        setAlertLevel(context, toastView, toastLevel);

        //每次创建Toast时先做一下判断
        //如果前面已经有Toast在显示，则只是更新toast内容，而不再创建，提升用户体验
        if (mToast != null) {
            TextView textView = toastView.findViewById(R.id.tv_toast);
            textView.setText(text);
        } else {
            mToast = Toast.makeText(context, text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
            TextView textView = toastView.findViewById(R.id.tv_toast);
            textView.setText(text);
        }

        mToast.setView(toastView);
        mToast.setGravity(Gravity.CENTER, 0, 0);

        try {
            Field mTNField = mToast.getClass().getDeclaredField("mTN");
            mTNField.setAccessible(true);
            Object mTNObject = mTNField.get(mToast);
            Class tnClass = mTNObject.getClass();
            Field paramsField = tnClass.getDeclaredField("mParams");
            paramsField.setAccessible(true);
            WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) paramsField.get(mTNObject);
            layoutParams.windowAnimations = R.style.pos_toast_anim;
        } catch (Exception e) {
            e.printStackTrace();
        }

        mToast.show();

    }

    @SuppressLint("ShowToast")
    public void makeShortToast(Context context, String text) {
        makeToast(context, ToastType.TOAST_TYPE_RECTANGLE, ToastRectangleLevel.ALERT_GUIDE_LEVEL_TWO, text, false);
    }

    @SuppressLint("ShowToast")
    public void makeLongToast(Context context, String text) {
        makeToast(context, ToastType.TOAST_TYPE_RECTANGLE, ToastRectangleLevel.ALERT_GUIDE_LEVEL_TWO, text, true);
    }

    @SuppressLint("ShowToast")
    public void makeCheckMarkToast(Context context, String toastMsg) {
        makeToast(context, ToastType.TOAST_TYPE_SQUARE, ToastSquareLevel.ALERT_CHECK_MARK_SQUARE, toastMsg, false);
    }


    @SuppressLint("ShowToast")
    public void makeSuccessToast(Context context, String toastMsg) {
        makeToast(context, PosToast.ToastType.TOAST_TYPE_SQUARE, PosToast.ToastSquareLevel.ALERT_SUCCESS_SQUARE, toastMsg, false);
    }

}
